package com.company.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.common.aop.annotation.LogAnnotation;
import com.company.project.entity.BusiPatrolEntity;
import com.company.project.vo.req.MytraRequestVO;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import com.company.project.common.utils.DataResult;

import com.company.project.entity.BusiMytraEntity;
import com.company.project.service.BusiMytraService;



/**
 * 我的轨迹
 *
 * @author zhanggm
 * @email *****@mail.com
 * @date 2021-06-15 09:54:41
 */
@Controller
@RequestMapping("/")
public class BusiMytraController {
    @Autowired
    private BusiMytraService busiMytraService;


    /**
    * 跳转到页面
    */
    @GetMapping("/index/busiMytra")
    public String busiMytra() {
        return "busimytra/list";
        }

    @ApiOperation(value = "新增")
    @PostMapping("busiMytra/add")
    @RequiresPermissions("busiMytra:add")
    @LogAnnotation(title = "我的轨迹", action = "新增")
    @ResponseBody
    public DataResult add(@RequestBody BusiMytraEntity busiMytra){
        busiMytraService.wholeAdd(busiMytra);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("busiMytra/delete")
    @RequiresPermissions("busiMytra:delete")
    @LogAnnotation(title = "我的轨迹", action = "删除")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        busiMytraService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("busiMytra/update")
    @RequiresPermissions("busiMytra:update")
    @LogAnnotation(title = "我的轨迹", action = "更新")
    @ResponseBody
    public DataResult update(@RequestBody BusiMytraEntity busiMytra){
        busiMytraService.updateById(busiMytra);
        return DataResult.success();
    }
    @ApiOperation(value = "查询全部数据")
    @PostMapping("busiMytra/findList")
    @RequiresPermissions("busiMytra:list")
    @LogAnnotation(title = "我的轨迹", action = "查询全部数据")
    @ResponseBody
    public DataResult findList(@RequestBody MytraRequestVO mytraRequestVO) {
        return DataResult.success(busiMytraService.wholeGetMytralist(mytraRequestVO));
    }
    @ApiOperation(value = "个人轨迹查询")
    @PostMapping("busiMytra/getOnetra")
    @RequiresPermissions("busiMytra:list")
    @LogAnnotation(title = "我的轨迹", action = "个人轨迹查询")
    @ResponseBody
    public DataResult getOnetra(@RequestBody MytraRequestVO mytraRequestVO) {
        return DataResult.success(busiMytraService.wholeGetMytralist(mytraRequestVO));
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("busiMytra/listByPage")
    @RequiresPermissions("busiMytra:list")
    @LogAnnotation(title = "我的轨迹", action = "查询分页数据")
    @ResponseBody
    public DataResult findListByPage(@RequestBody BusiMytraEntity busiMytra){
        Page page = new Page(busiMytra.getPage(), busiMytra.getLimit());
        LambdaQueryWrapper<BusiMytraEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        queryWrapper.eq(BusiMytraEntity::getUserId, busiMytra.getUserId());
        // 起始时间
        String startTime = busiMytra.getStartTime();
        String endTime = busiMytra.getEndTime();
        if (StringUtils.isNotBlank(startTime)) {
            queryWrapper.gt(BusiMytraEntity::getCreateTime, startTime);
        }
        if (StringUtils.isNotBlank(endTime)) {
            queryWrapper.lt(BusiMytraEntity::getCreateTime, endTime);
        }
        queryWrapper.orderByDesc(BusiMytraEntity::getCreateTime);
        IPage<BusiMytraEntity> iPage = busiMytraService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }
    @ApiOperation(value = "获取人员位置")
    @PostMapping("busiMytra/personPos")
    @RequiresPermissions("busiMytra:list")
    @LogAnnotation(title = "我的轨迹", action = "获取人员位置")
    @ResponseBody
    public DataResult personPos(@RequestBody BusiMytraEntity busiMytra){
        return DataResult.success(busiMytraService.wholeGetPos(busiMytra.getUserId()));
    }

}
